
var express = require('express');
var router = express.Router();
var connection = require('../mysql')
const { packRes } = require("../utils/packRes");



router.get('/type', function(req, res, next) {
    connection.query('SELECT * FROM link_type', function(error, results, fields) {
        if (error) throw error;
        res.status(200).send(packRes(200, {results}));
        return;
    });
});

router.post('/type/add', function(req, res, next) {
    const {typename} = req.body;
    connection.query('INSERT INTO link_type (typename) VALUES (?)',
        [typename], function(error, results, fields) {
            if (error) throw error;
            res.status(200).send(packRes(200,{typename}));
            return;
        });
});``

router.post('/type/delete/:id', function(req, res, next) {
    const id = req.params.id;
    console.log('删除接口',id)
    connection.query('DELETE FROM link_type WHERE id = ?',[id], function(error, results, fields) {
        if (error) throw error;
        res.status(200).send(packRes(200,'删除成功'));
        return;
    });
});




router.get('/', function (req, res, next) {
    const { page = 1, limit = 5 } = req.query;
    const offset = (page - 1) * limit;
    connection.query('SELECT COUNT(*) as total FROM link', function (error, results, fields) {
        if (error) throw error;
        const total = results[0].total;
        connection.query('SELECT * FROM link LIMIT ?, ?', [offset, limit * 1], function (error, results, fields) {
            if (error) throw error;
            const count = results.length;
            const pages = Math.ceil(total / limit);
            res.status(200).send(packRes(200, { results, count, total, pages }));
            return;
        });
    });
});

router.post('/add', function(req, res, next) {
    const {title,target,type} = req.body;
    connection.query('INSERT INTO link (title,target,type) VALUES (?,?,?)',
        [title,target,type], function(error, results, fields) {
            if (error) throw error;
            res.status(200).send(packRes(200,{title,target,type}));
            return;
        });
});

router.post('/delete/:id', function(req, res, next) {
    const id = req.params.id;
    connection.query('DELETE FROM link WHERE id = ?',[id], function(error, results, fields) {
        if (error) throw error;
        res.status(200).send(packRes(200,'删除成功'));
        return;
    });
});
 
router.post('/update', function (req, res, next) {
    const { id,title, target, type } = req.body;
    const sql = `SELECT * FROM link WHERE id='${id}'`;
    connection.query(sql,(err,results)=>{
        if (err) throw error;
        if(!results[0]){
            res.status(200).send(packRes(400, null,'找不到该数据'));
        }
        const updateSQL = `UPDATE link SET  title = ?, target = ?, type = ? WHERE id = ?`;
        connection.query(updateSQL, [title, target, type,id], function (error) {
            console.log(22222222)
            if (error) throw error;
            res.status(200).send(packRes(200, { id, title, target }));
        });
    })
});



module.exports = router